package com.example.demo.buessiness.user.repository;

import com.example.demo.buessiness.user.model.po.User;
import msa.common.model.ResultModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserRepository extends JpaRepository<User,Long> {

    /**
     * 分页方法
     * @param spec
     * @param pageable
     * @return
     */
    Page<User> findAll(Specification<User> spec, Pageable pageable);

    /**
     * 获取所有demo 根据时间排序
     * @return
     */
    List<User> findAllByOrderByIdAsc();

    /**
     * 修改名称
     * @param username
     * @param id
     */
    @Modifying
    @Query(value = "update user p set p.username = ?1 where p.id = ?2",nativeQuery=true)
    void updateUsernameById(String username, Long id);


    /**
     * 登录验证
     * @param username
     * @param password
     */
    @Modifying
    @Query(value = "SELECT * FROM user WHERE username = ?1 AND password = ?2",nativeQuery=true)
    List<User> logByUsername(String username,String password);

    /**
     * 查询用户名
     * @param username
     */
    @Modifying
    @Query(value = "SELECT * FROM user WHERE username = ?1 ",nativeQuery=true)
    List<User> findByUsername(String username);

}
